(function(undefined) {

    (function(factory) {
        "use strict";

        // amd module
        if (typeof define === "function" && define.amd) {
            define(["jquery", "widget"], factory);
        }
        // global
        else {
            factory();
        }

    })
    (function($, widget) {
        "use strict";

        //渲染在foundationForm上使用的按钮
        function renderBtnInFoundationForm(obj, oOption, oAttr, oCss, auiCtx) {
            var render = function($widget, $selector, option, attr, css) {
                var init = function() {
                    $button = $selector.find('button');
                    if(auiCtx){
                       
                        $button.text($AW.nsl(option.name,attr.id,auiCtx )|| $AW.nsl(attr.desp,attr.id,auiCtx ));
                    }else{
                        
                        $button.text(obj.nsl(option.name) || obj.nsl(attr.desp));
                    }
                    
                    attr.id && $button.attr('id', attr.id);
                    attr['data-authority'] && $button.attr('data-authority', attr['data-authority']);
                    option.status == 'disabled' && $button.attr('disabled', option.status);

                    if (useIcon && icon) {
                        var iconTmp = '<span><i class="' + icon + '"></i></span>';
                        var btnText = $button.html();
                        //图标加在右边
                        if (iconPosition == 1) {
                            $button.html(btnText + iconTmp);
                            $('span', $button).css('padding-left', '10px');
                        } else {
                            //图标加在左边
                            $button.html(iconTmp + btnText);
                            $('span', $button).css('padding-right', '10px');
                        }
                    }
                    if (btnLocation) {
                        $button.closest('div').css('text-align', btnLocation);
                    }
                };
                var classname,
                    btnTemplate = "<button class='btn' type='button'></button>",
                    classRegExp = /medium-\d+/,
                    $button,
                    icon,
                    useIcon,
                    iconPosition,
                    borderHColor,borderAColor,
                    btnLocation, style,borderObj,btnClass;

                option.icon && (icon = option.icon);
                option.useIcon && (useIcon = option.useIcon);
                option.iconPosition && (iconPosition = option.iconPosition);
                option.btnLocation && (btnLocation = option.btnLocation);

                $selector.append(btnTemplate);

                if ($widget && $widget.length) {
                    $widget.find('div.aui-widget-title').css('text-align', 'left');

                    classname = $widget.attr("class");

                    if (classRegExp.test(classname)) {
                        $widget.attr("class", classname.replace(classRegExp, "medium-" + option.span));
                    } else {
                        $widget.addClass("medium-" + option.span);
                    }

                    $widget.addClass("columns");

                    init();
                } else {
                    classname = $selector.attr("class");

                    if (classRegExp.test(classname)) {
                        $selector.attr("class", classname.replace(classRegExp, "medium-" + option.span));
                    } else {
                        $selector.addClass("medium-" + option.span);
                    }

                    $selector.addClass("columns");

                    init();
                }
                $button = $selector.find('button');
                //样式处理
                if (css && css.theme) {
                    if(css.theme['function']){
                        $button.removeClass().addClass('btn ' + css.theme['function']);
                    }
                }
                if (css && (style = css.style) && style.border) {
                    borderObj = JSON.parse(JSON.stringify(style.border));
                    btnClass = $button.prop('className').split(' ')[1];
                    if(style.border && !$.isEmptyObject(borderObj)&& borderObj['border-color']) {
                        style.border = $.extend(true, {}, style.border, {'border-color': borderObj['border-color'] + '!important'});
                    }

                    if(btnClass){
                        $AW.cssHover('.btn.' + btnClass, $selector, $.extend({}, style.border, style.font, style.size, style.backgroundColor), '');
                        $AW.cssHover('.btn.'+btnClass+' span i',$selector,style.icon,'');
                    }else{
                        $AW.cssHover('.btn', $selector, $.extend({}, style.icon, style.border, style.font, style.size, style.backgroundColor), '');
                        $AW.cssHover('.btn'+' span i',$selector,style.icon,'');
                    }

                    if(style.hoverBtn) {
                        if(borderHColor){
                            style.hoverBtn = borderHColor && $.extend(true, {}, style.hoverBtn, {'border-color': borderHColor+ '!important'});
                        }
                        if(btnClass){
                            $AW.cssHover('.btn.' + btnClass, $selector, style.hoverBtn, ':hover');
                        }else{
                            $AW.cssHover('.btn',$selector,style.hoverBtn,':hover');
                        }
                    }

                    if(style.activeBtn){
                        borderAColor =JSON.parse(JSON.stringify(style.activeBtn))['border-color'];
                        style.activeBtn =borderAColor && $.extend(true, {}, style.activeBtn, {'border-color': borderAColor+ '!important'});
                        if(btnClass){
                            $AW.cssHover('.btn.' + btnClass, $selector, style.activeBtn, ':active');
                        }else{
                            $AW.cssHover('.btn',$selector,style.activeBtn,':active');
                        }
                    }
                }


            };

            var oWidget, $widget, attr, option, css,
                $selector,
                template = "<div data-widget-type='aweb4FoundationButton'></div>";

            if (obj.version) {
                oWidget = obj;
                $widget = oWidget[0].$widget;

                /* 容器类组件需删除这段代码--start */
                $widget.children(':gt(1)').remove();
                $widget.append(template);
                /* 容器类组件需删除这段代码--end */

                $selector = $widget.children(':last');
                option = oWidget.option();
                attr = oWidget.attr();
                css = oWidget.css();

                /*编辑阶段的渲染代码*/
                render($widget, $selector, option, attr, css,auiCtx,oWidget);
            } else {
                $selector = obj;

                //把默认的模板替换掉
                $selector.after(template);
                var $template = $selector.next();
                $selector.remove();
                $selector = $template;

                option = oOption;
                attr = oAttr;
                css = oCss;

                /*编译阶段渲染代码*/
                render($widget, $selector, option, attr, css,auiCtx,oWidget);
            }
        }

        //创建默认的按钮
        function renderDefaultBtn(obj, oOption, oAttr, oCss, auiCtx) {
            var oWidget, $widget, $selector, option, $button, css, style,btnClass,borderObj,borderHColor,borderAColor;

            if (obj.version) {
                oWidget = obj;
                oWidget.display(true);
                $widget = oWidget[0].$widget;
                $selector = $widget.children(':last');
                css = oWidget.css();
                $button = $widget.find('button');
                $button.html('');
                //标题靠左
                $widget.find('div.aui-widget-title').css('text-align', 'left');


                if (option = oWidget.option()) {
                    //处理按钮位置
                    if (option.btnLocation) {
                        $widget.parent('div').css('text-align', option.btnLocation);
                    }

                    $button.text(oWidget.nsl(option.name) || oWidget.nsl('按钮'));
                    if (option.useIcon && option.icon) {
                        var iconTmp = '<span><i class="' + option.icon + '"></i></span>';
                        var btnText = $button.html();
                        //图标加在右边
                        if (option.iconPosition === 1) {
                            $button.html(btnText + iconTmp);
                            $('span', $button).css('padding-left', '10px');
                        } else {
                            //图标加在左边
                            $button.html(iconTmp + btnText);
                            $('span', $button).css('padding-right', '10px');
                        }
                    }
                }

                $button[option.status === 'disabled' ? 'attr' : 'removeAttr']('disabled', 'true');


            } else {
                option = oOption;
                css = oCss;
                $button = obj;
                $widget = obj.parent();
                if (option) {
                    //处理按钮位置
                    if (option.btnLocation) {
                        $button.parent('div').css('text-align', option.btnLocation);
                    }
                    $button.text($AW.nsl(option.name,$button.attr('id'),auiCtx) || $AW.nsl('按钮',$button.attr('id'),auiCtx));

                    if (option.useIcon && option.icon) {
                        var iconTmp = '<span><i class="' + option.icon + '"></i></span>';
                        var btnText = $button.html();
                        //图标加在右边
                        if (option.iconPosition === 1) {
                            $button.html(btnText + iconTmp);
                            $('span', $button).css('padding-left', '10px');
                        } else {
                            //图标加在左边
                            $button.html(iconTmp + btnText);
                            $('span', $button).css('padding-right', '10px');
                        }
                    }
                }

                if (option.status === 'disabled') {
                    $button.attr('disabled', 'true');
                }

            }
            //样式处理
            if (!$.isEmptyObject(css) && css.theme) {
                css.theme['function']  && $button.removeClass().addClass('btn ' + css.theme['function']);
            }

            if (!$.isEmptyObject(css) && (style = css.style)) {

                if(btnClass = $button.prop('className')){
                    btnClass=btnClass.split(' ')[1];
                }

                if(style.border) {
                    borderObj = JSON.parse(JSON.stringify(style.border));
                    if(!$.isEmptyObject(borderObj) && borderObj['border-color']){
                        style.border =  $.extend(true, {}, style.border, {'border-color': borderObj['border-color'] + '!important'});
                    }
                }
                if(btnClass){
                    $AW.cssHover('.btn.' + btnClass, $button, $.extend({}, style.icon, style.border, style.font, style.size, style.backgroundColor), '',true);
                    $AW.cssHover('.btn.'+btnClass+' span i',$button,style.icon,'',true);
                }else{
                    $AW.cssHover('.btn', $button, $.extend({}, style.icon, style.border, style.font, style.size, style.backgroundColor), '',true);
                    $AW.cssHover('.btn'+' span i',$button,style.icon,'',true);
                }
                if(style.hoverBtn) {
                    borderHColor =JSON.parse(JSON.stringify(style.hoverBtn))['border-color'];
                    if(borderHColor){
                        style.hoverBtn =borderHColor && $.extend(true, {}, style.hoverBtn, {'border-color':borderHColor + '!important'});
                    }
                    if(btnClass){
                        $AW.cssHover('.btn.' + btnClass, $button, style.hoverBtn, ':hover',true);
                    }else{
                        $AW.cssHover('.btn',$button,style.hoverBtn,':hover',true);
                    }
                }
                if(style.activeBtn){
                    borderAColor =JSON.parse(JSON.stringify(style.activeBtn))['border-color'];
                    style.activeBtn =borderAColor && $.extend(true, {}, style.activeBtn, {'border-color':borderAColor  + '!important'});
                    if(btnClass){
                        $AW.cssHover('.btn.' + btnClass, $button, style.activeBtn, ':active',true);
                    }else{
                        $AW.cssHover('.btn', $button, style.activeBtn, ':active',true);
                    }
                }

            }
        }

        //根据容器决定渲染方法
        function determineRenderByCtn(obj, oOption, oAttr, oCss, auiCtx) {
            var $widget, $selector, $container, widgetType, ctnRenderMethodMap;
            if (obj.version) {
                $widget = obj[0].$widget;

                $selector = $widget.children(':last');
                //获取父容器的类型
                $container = $selector.parent('div').parent('div');
                widgetType = $container.attr('data-widget-type');

                ctnRenderMethodMap = obj[0].data.option.ctnRenderMethodMap;
            } else {
                $selector = obj;
                //获取父容器的类型
                $container = $selector.parent('div');
                widgetType = $container.attr('data-widget-type');

                ctnRenderMethodMap = auiCtx.configs[oAttr.id].ctnRenderMethodMap;
            }

            var otherRenderMethod,
                isFinded = false;
            for (var ctn in ctnRenderMethodMap) {
                if ('_other_' === ctn) {
                    otherRenderMethod = ctnRenderMethodMap[ctn];
                    continue;
                }
                if (widgetType === ctn) {
                    eval('(' + ctnRenderMethodMap[ctn] + '(obj, oOption, oAttr, oCss, auiCtx))');
                    isFinded = true;
                    break;
                }
            }
            //没找到则使用配置的_other_进行渲染
            if (!isFinded && otherRenderMethod) {
                eval('(' + otherRenderMethod + '(obj, oOption, oAttr, oCss, auiCtx))');
            }

        }

        if(!widget.component.btn){
            widget.component.btn={};
        }

        widget.component.btn.normalBtn = function (obj, oOption, oAttr, oCss, auiCtx) {
            determineRenderByCtn(obj, oOption, oAttr, oCss, auiCtx);


            return {
                display: function (result, input1, input2, condition) {
                    this[result ? 'hide' : 'show']();
                },
                show: function () {
                    obj.removeClass('hide';);
                },
                hide: function () {
                    obj.addClass('hide');
                },
                getter: function () {
                    return {
                        icon:oOption.icon,
                        name:oOption.name,
                        btnLocation:oOption.btnLocation,
                        iconPosition:oOption.iconPosition

                    };
                },
                setter: function (data) {
                    oOption.icon = data.icon;
                    oOption.name = data.name;
                    oOption.btnLocation = data.btnLocation;
                    oOption.iconPosition = data.iconPosition;
                    determineRenderByCtn(obj, oOption, oAttr, oCss, auiCtx);
                },
                reset:function(e) {
                    app.reset(obj.closest('form'));
                }

            };
        };

        return widget;
    });
})();
auiCtx.variables.foundationDatepicker1.setValue(data);
